package com.tvshows.front.view.main;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Shell;

import com.tvshows.front.factory.TypeUI;
import com.tvshows.front.factory.ViewFactory;
import com.tvshows.front.ui.favorite.FavoriteUI;
import com.tvshows.front.ui.main.MainUI;
import com.tvshows.front.ui.menu.MenuUI;
import com.tvshows.front.ui.recap.RecapUI;

/**
 * @author tMidy
 * <p>
 * The main view : regroups all the main composites
 * </p>
 */
public class MainView
    implements MainUI
{

    /**
     * Shell width
     */
    private static final Integer SIZE_WIDTH = 800;

    /**
     * Shell Height
     */
    private static final Integer SIZE_HEIGHT = 600;

    /**
     * form data measure
     */
    private static final Integer DATA_HUNDRED = 100;

    /**
     * form data measure
     */
    private static final Integer DATA_SEVENTY = 70;

    /**
     * The display
     */
    private final Display display;

    /**
     * Sash data for the {@link FavoriteUI}
     */
    private FormData favorisData;

    /**
     * Sash data for {@link RecapUI}
     */
    private FormData recapData;

    /**
     * The main Shell
     */
    private Shell shell;

    /**
     * The {@link FavoriteUI}
     */
    private FavoriteUI favoriteView;

    /**
     * The {@link RecapUI}
     */
    private RecapUI recapView;

    /**
     * Cosntructor
     */
    public MainView()
    {
        Display.setAppName( "Tv Shows" );
        display = new Display();
        initShell();
        initMenus();
    }

    /**
     * @see com.tvshows.front.ui.main.MainUI#waitForNextFrame()
     */
    @Override
    public void waitForNextFrame()
    {
        while ( !shell.isDisposed() )
        {
            if ( !display.readAndDispatch() )
            {
                display.sleep();
            }
        }
        display.close();
    }

    /**
     * @see com.tvshows.front.ui.FrontUI#initComposites()
     */
    @Override
    public void initComposites()
    {
        favoriteView.setLayoutData( favorisData );
        recapView.setLayoutData( recapData );
    }

    /**
     * Configure the shell to center the frame in teh screen
     */
    public void initShell()
    {
        shell = new Shell( display, SWT.CLOSE | SWT.MIN );
        shell.setSize( SIZE_WIDTH, SIZE_HEIGHT );
        shell.setText( "Tv Shows" );
        shell.setLayout( new FormLayout() );

        // Petite operation pour placer le shell au milieu
        Monitor primary = display.getPrimaryMonitor();
        Rectangle bounds = primary.getBounds();
        Rectangle rect = shell.getBounds();
        int x = bounds.x + ( bounds.width - rect.width ) / 2;
        int y = bounds.y + ( bounds.height - rect.height ) / 2;

        shell.setLocation( x, y );
    }

    /**
     * Configures the frame menus
     */
    public void initMenus()
    {
        MenuUI menuView = (MenuUI) ViewFactory.getUI( TypeUI.MENU_VIEW );
        menuView.initMenus( this );
    }

    /**
     * @see com.tvshows.front.ui.FrontUI#initFormData()
     */
    @Override
    public void initFormData()
    {
        /*
         * The main sash
         */
        Sash sash = new Sash( shell, SWT.VERTICAL );
        /*
         * The main sashData
         */
        FormData sashData = new FormData();
        sashData.left = new FormAttachment( DATA_SEVENTY, 0 );
        sashData.top = new FormAttachment( 0, 0 );
        sashData.bottom = new FormAttachment( DATA_HUNDRED, 0 );
        favorisData = new FormData();
        favorisData.left = new FormAttachment( sash, 0 );
        favorisData.right = new FormAttachment( DATA_HUNDRED, 0 );
        favorisData.top = new FormAttachment( 0, 0 );
        favorisData.bottom = new FormAttachment( DATA_HUNDRED, 0 );
        recapData = new FormData();
        recapData.left = new FormAttachment( 0, 0 );
        recapData.right = new FormAttachment( sash, 0 );
        recapData.top = new FormAttachment( 0, 0 );
        recapData.bottom = new FormAttachment( DATA_HUNDRED, 0 );
        sash.setLayoutData( sashData );
    }

    /**
     * @see com.tvshows.front.ui.main.MainUI#getRecapData()
     */
    @Override
    public FormData getRecapData()
    {
        return recapData;
    }

    /**
     * @see com.tvshows.front.ui.FrontUI#getShell()
     */
    @Override
    public Shell getShell()
    {
        return this.shell;
    }

    /**
     * @see com.tvshows.front.ui.main.MainUI#getFavoritesData()
     */
    @Override
    public FormData getFavoritesData()
    {
        return favorisData;
    }

    /**
     * @see com.tvshows.front.ui.main.MainUI#layout()
     */
    @Override
    public void layout()
    {
        shell.layout();
        shell.open();
        shell.setEnabled( true );
        waitForNextFrame();
        display.dispose();
    }

    /**
     * @see com.tvshows.front.ui.main.MainUI#setFavoriteView(com.tvshows.front.ui.favorite.FavoriteUI)
     */
    @Override
    public void setFavoriteView( FavoriteUI favoriteView )
    {
        this.favoriteView = favoriteView;
    }

    /**
     * @see com.tvshows.front.ui.main.MainUI#setRecapView(com.tvshows.front.ui.recap.RecapUI)
     */
    @Override
    public void setRecapView( RecapUI recapView )
    {
        this.recapView = recapView;

    }

    /**
     * @return the favoriteView
     */
    @Override
    public FavoriteUI getFavoriteView()
    {
        return favoriteView;
    }

    /**
     * @return the recapView
     */
    @Override
    public RecapUI getRecapView()
    {
        return recapView;
    }

}
